<?php
namespace modules\dashboard;
use \modules\ui;
use \modules\org\api as org;
use \modules\auth\api as auth;
use \modules\potential\page as potential;

class page
{
    static function mapping()
    {
        return [
            '/' => 'dashboard',
        ];
    }

    static function dashboard($q, $html, &$args)
    {
        $user = auth::validate($q, $args);
        $org_id = $user['org'];

        $args['page'] = 1;
        $potential = $user["role"] === "admin"
            ? null
            : potential::list_my($q, $html, $args);

        $org = org::view($q, $args);
        $args['SUBTITLE'] = "信息中心";
        $args['HEADER-CONTROL'] = ui::header_control_with_logout($q, $args, $html);

        return $html->mark(['.',
            ['div', 'class' => "x-split-2-1",
                ui::card(ui::readonly_form($html, [
                    ['longtext', '组织备注', $org['comment']],
                    ['text'    , '用户姓名', $user['name']],
                    ['enum'    , '用户角色', [$user['role'], 'admin' => '管理员', 'salesman' => '业务员'], 'name' => 'role'],
                ]), $org['name']),
                ui::card(ui::readonly_form($html, [
                    ['int', '在职用户数'  , api::counts($q, $org_id, 'users')],
                    ['int', '有效联系人数', api::counts($q, $org_id, 'contacts')],
                    ['int', '有效商机数'  , api::counts($q, $org_id, 'potentials')],
                ]), '数据信息')
            ],
            $potential,
        ]);
    }
}

